/*
 * Copyright 2012 Oracle and/or its affiliates.
 * All rights reserved.  You may not modify, use,
 * reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * http://developers.sun.com/license/berkeley_license.html
 */


/*
 * Copyright 2009 Sun Microsystems, Inc.
 * All rights reserved.  You may not modify, use,
 * reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * http://developer.sun.com/berkeley_license.html
 */
package cart.client;

import cart.ejb.App;
import javax.ejb.EJB;
import cart.ejb.Cart;
import java.util.Date;
import javax.annotation.security.RolesAllowed;


/**
 *
 * The client class for the CartBean example. Client adds books to the cart,
 * prints the contents of the cart, and then removes a book which hasn't been
 * added yet, causing a BookException.
 * @author ian
 */

public class CartClient {
    @EJB
    private static Cart cart;
    
    @EJB
    private static App app;
    //@Resource SessionContext ctx;
    //@Resource SecurityContext ctx;
    
    public CartClient(String[] args) {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        CartClient client = new CartClient(args);
        client.doTest();
    }
    
    @RolesAllowed("users")
    public void doTest() {
        try {
            System.out.println("Start...");
            System.out.println("Cart Id ="+cart.getId() +","+ cart.getName());
            String name = new Date().toString();
            System.out.println("setName("+name+")");
            cart.setName(name);
            System.out.println();
            System.out.println("app.getAppId() = "+app.getAppId());
            System.out.println("app.getAppInstanceNum() = "+app.getAppInstanceNum());
            System.out.println("cart.getId() = "+cart.getId());
            System.out.println("app.getCartInstanceNum() = "+app.getCartInstanceNum());            
            
            javax.swing.JOptionPane.showMessageDialog(null, "Cart Id ="+cart.getId() +","+ cart.getName());

            javax.swing.JOptionPane.showMessageDialog(null, "Cart Id ="+cart.getId() +","+ cart.getName());
           
            System.exit(0);
        }  catch (Exception ex) {
            System.err.println("Caught an unexpected exception!");
            ex.printStackTrace();
            System.exit(1);
        }
    }
}
